﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Text; //引入：StringBuilder
using System.Text.RegularExpressions; //正则表达式

namespace webnew.Common
{
    public static class CommonHelper
    {
        #region 获取系统配置
        /// <summary>
        /// 获取系统使用公司
        /// </summary>
        public static string GetCommpanyName()
        {
            return ConfigurationSettings.AppSettings["CompanyName"];
        }

        /// <summary>
        /// 获取系统名称
        /// </summary>
        public static string GetSystemName()
        {
            return ConfigurationSettings.AppSettings["SystemName"];
        }

        /// <summary>
        /// 获取菜单父节点ID
        /// </summary>
        public static string GetMenuParentID()
        {
            return ConfigurationSettings.AppSettings["MenuParentID"];
        }
        #endregion

        #region 获取日期
        /// <summary>
        /// 获取系统日期，带星期
        /// </summary>
        public static string GetSystemDate()
        {
            return DateTime.Today.ToLongDateString()+" 星期"+ getWeekday(DateTime.Today.DayOfWeek);
        }

        /// <summary>
        /// 返回星期几
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string getWeekday(DayOfWeek d)
        {
            string result;
            switch (d)
            {
                case DayOfWeek.Friday:
                    result = "五";
                    break;
                case DayOfWeek.Monday:
                    result = "一";
                    break;
                case DayOfWeek.Saturday:
                    result = "六";
                    break;
                case DayOfWeek.Sunday:
                    result = "天";
                    break;
                case DayOfWeek.Thursday:
                    result = "四";
                    break;
                case DayOfWeek.Tuesday:
                    result = "二";
                    break;
                case DayOfWeek.Wednesday:
                    result = "三";
                    break;
                default:
                    result = "一";
                    break;
            }
            return result;
        }
        #endregion

        #region MD5
        /// <summary>
        /// MD5加密
        /// </summary>
        public static string md5(string str, int code)
        {
            if (code == 16) //16位MD5加密（取32位加密的9~25字符） 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }
            else//32位加密 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }
        }
        #endregion

        #region 常用数据绑定方法
        /// <summary>
        /// 数据绑定GridView控件
        /// </summary>
        public static void DataGrdBind(GridView Grdview,Object Lst)
        {
            Grdview.DataSource = Lst;
            Grdview.DataBind();
        }

        /// <summary>
        /// 数据绑定DropDownList控件
        /// </summary>
        public static void DataDdlBind(DropDownList Ddl, object Lst,string TextField,string ValueField)
        {
            Ddl.DataSource = Lst;
            Ddl.DataTextField = TextField;
            Ddl.DataValueField = ValueField;
            Ddl.DataBind();
        }

        /// <summary>
        /// 遍历DropDownList,使其选中对应值
        /// </summary>
        public static void DdlFind(DropDownList Ddl,string FindStr)
        {
            for (int i = 0; i < Ddl.Items.Count; i++)
            {
                if (Ddl.Items[i].Value == FindStr)
                {
                    Ddl.SelectedIndex = i;
                    break;
                }
            }
        }

        /// <summary>
        /// 所有列表控件绑定
        /// </summary>
        public static void DataLbBind(ListControl lstControl, object Lst, string TextField, string ValueField)
        {
            lstControl.DataSource = Lst;
            lstControl.DataTextField = TextField;
            lstControl.DataValueField = ValueField;
            lstControl.DataBind();
        }
        #endregion

        #region 截取中英文字符串

        /// <summary>
        /// C#获取指定长度中英文字符串长度
        /// </summary>
        /// <param name="stringToSub">被截取的字符串</param>
        /// <returns></returns>
        public static int GetStringLength(string stringToSub)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]);
                    nLength = nLength + 1;
                }           
            }
            return nLength;
        }

        /// <summary>
        /// C#截取指定长度中英文字符串方法
        /// </summary>
        /// <param name="stringToSub">被截取的字符串</param>
        /// <param name="length">长度</param>
        /// <param name="fill">尾部附加字符串</param>
        /// <returns></returns>
        public static string GetCnString(string stringToSub, int length, string fill)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            bool isCut = false;
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]);
                    nLength = nLength + 1;
                }
                if (nLength > length)
                {
                    isCut = true;
                    break;
                }
            }
            if (isCut)
                return sb.ToString() + fill;
            else
                return sb.ToString();
        }
        #endregion

        #region gridView单元格设置
        /// <summary>
        /// 超过字符长度则隐藏,显示部分内容,在ToolTip显示全部内容
        /// </summary>
        /// <param name="Tabcell">单元格控件</param>
        /// <param name="strLength">字符长度</param>
        public static void grdCellStyle(Control Tabcell, int strLength)
        {
            if (Tabcell is System.Web.UI.WebControls.TextBox)
            {
                if (GetStringLength(((TextBox)Tabcell).Text) > strLength)
                {
                    ((TextBox)Tabcell).ToolTip = ((TextBox)Tabcell).Text;
                    ((TextBox)Tabcell).Text = GetCnString(((TextBox)Tabcell).Text, strLength, "...");
                }
            }
            else if (Tabcell is System.Web.UI.WebControls.TableCell)
            {
                if (((TableCell)Tabcell).Text.Length > strLength)
                {
                    ((TableCell)Tabcell).ToolTip = ((TableCell)Tabcell).Text;
                    ((TableCell)Tabcell).Text = GetCnString(((TableCell)Tabcell).Text, strLength, "...");
                }
            }
            else if (Tabcell is System.Web.UI.WebControls.Label)
            {
                if (((Label)Tabcell).Text.Length > strLength)
                {
                    ((Label)Tabcell).ToolTip = ((Label)Tabcell).Text;
                    ((Label)Tabcell).Text = GetCnString(((Label)Tabcell).Text, strLength, "...");
                }
            }

        }


        #endregion

        #region gridView单元格验证数据有效性
        /// <summary>
        /// GrdView验证数据有效性
        /// </summary>
        /// <param name="grd">GrdView</param>
        /// <param name="strGrdValue">指定列 控件名</param>
        /// <param name="strValidType">验证 类型</param>
        /// <param name="isNull">是否为空</param>
        /// <returns></returns>
        public static string GrdViewValidData(GridView grd, string strGrdValue, string strValidType, bool isNull)
        {
            string strReturnValue = "";
            foreach (GridViewRow grow in grd.Rows)
            {
                string strValve = ((TextBox)grow.FindControl(strGrdValue)).Text;
                if (!isNull)
                {
                    if (strValve == "")
                    {
                        strReturnValue = "0";
                        if (strReturnValue == "0") break;
                    }
                    else
                    {
                        strReturnValue = ValidType(strValve, strValidType);
                        if (strReturnValue == "0") break;
                    }
                }
                else
                {
                    strReturnValue = ValidType(strValve, strValidType);
                    if (strReturnValue == "0") break;
                }
            }
            return strReturnValue;
        }

        /// <summary>
        /// 不同类型的验证方法
        /// </summary>
        /// <param name="strValve">验证的数据</param>
        /// <param name="strValidType">验证类型</param>
        /// <returns></returns>
        public static string ValidType(string strValve, string strValidType)
        {
            string strReturnValue = "";
            switch (strValidType)
            {
                case "整数":
                    if (!DataValid.CheckInt32(strValve)) strReturnValue = "0";
                    else strReturnValue = "1";
                    break;
                case "浮点数":
                    if (!DataValid.CheckDouble(strValve)) strReturnValue = "0";
                    else strReturnValue = "1";
                    break;
                case "时间":
                    if (!DataValid.CheckDateTime(strValve)) strReturnValue = "0";
                    else strReturnValue = "1";
                    break;
                default:
                    break;
            }
            return strReturnValue;
        }
        #endregion
    }
}
